<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商户名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商户名称" clearable />
      </el-form-item>
      <el-form-item label="商户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择商户类型" clearable size="small">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.app_account_type)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入商户编码" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
         <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_enable_disable_status)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  size="small" @click="getTableData">搜索</el-button>
        <el-button icon="el-icon-refresh"  size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
      </el-form-item>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain type="primary" icon="el-icon-plus"  size="small" @click="openDialog('1')">新增企业商户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain type="primary" icon="el-icon-plus"  size="small" @click="openDialog('2')">新增个人商户</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getTableData"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="操作" align="center" fixed="left" width="100">
        <template v-slot="scope">
          <el-button size="small" icon="el-icon-edit" type="text" @click="handleUpdate(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
       <el-table-column label="商户名称" prop="name" align="center" fixed="left" />
      <el-table-column label="商户编码" prop="code" align="center" />
      <el-table-column label="商户类型" prop="type" align="center">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.type" />
        </template>
      </el-table-column>
     
      <el-table-column label="商户联系人" prop="userName" align="center" />
      <el-table-column label="联系方式" prop="phone" align="center" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="账户名" prop="accountName" align="center" />
      <el-table-column label="账号" prop="account" align="center" />
      <el-table-column label="开户行" prop="bank" align="center" />
      <el-table-column label="社会信用代码" prop="creditCode" align="center" />
      <el-table-column label="状态" prop="status" align="center">
        <template v-slot="scope">
          <el-switch @change="changeSwitch(scope.row)" v-model="scope.row.status" 
             :active-value="0" :inactive-value="1" />
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime" align="center">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getTableData" />
      
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="900px">
      <Merchant ref="merchantRefs" @changeVisible="changeVisible" v-if="dialogType == 1" :params="params" />
      <Person ref="personRefs" @changeVisible="changeVisible" v-if="dialogType == 2" :params="params" />
    </el-dialog>
  </div>
</template>

<script>
import { getBusinessList, updateBusinessInfo } from "@/api/merchantManagement/business";
import Merchant from './components/merchant.vue'
import Person from './components/person.vue'
export default {
  name: "business",
  components: {
    Merchant,
    Person
  },
  data() {
    return {
      showSearch:true,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name:null,
        type:null,
        code:null,
        status:null
      },
      statusList: [
        { name: "禁用", id: 0 },
        { name: "启用", id: 1 },
      ],
      typeList: [
        { name: "个人", id: 2 },
        { name: "企业", id: 1 },
      ],
      total: 0,
      loading: true,
      tableData: [],
      dialogVisible: false,
      dialogTitle: '',
      dialogType: 1,
      params: {}
    };
  },
  created() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const { data } = await getBusinessList(this.queryParams);
      this.loading = false
      this.tableData = data.list
      this.total = data.total
    },
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.getTableData()
    },
    openDialog(data) {
      this.dialogVisible = true
      if (data == 1) {
        this.dialogTitle = '企业商户'
        this.dialogType = 1
        this.$nextTick(() => {
          this.$refs.merchantRefs.initDialog()
        })
      }
      if (data == 2) {
        this.dialogTitle = '个人商户'
        this.dialogType = 2
        this.$nextTick(() => {
          this.$refs.personRefs.initDialog()
        })
      }
    },
    changeVisible(data) {
      this.dialogVisible = data
      this.getTableData()
    },
    handleUpdate(data) {
      this.params = data
      if (data.type == 1) {
        this.dialogTitle = '企业商户'
        this.dialogType = 1
        this.$nextTick(() => {
          this.$refs.merchantRefs.isValue()
        })
      }
      if (data.type == 2) {
        this.dialogTitle = '个人商户'
        this.dialogType = 2
        this.$nextTick(() => {
          this.$refs.personRefs.isValue()
        })
      }
      this.dialogVisible = true
    },
    changeSwitch(data) {
      updateBusinessInfo(data).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        });
        this.getTableData()
      })
    }
  },
};
</script>

<style lang="less" scoped></style>
